﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GroupManager.Resource.CSDL;
namespace GroupManager.Class
{
    public class LopAnh
    {
        // Kiểm tra ảnh đại diện;
        public static bool KiemTraAnhDaiDien(int maanh, int album)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            Album ab = db.Albums.SingleOrDefault(q => q.MaAlbum == album);
            if (ab.AnhDaiDien == maanh)
                return true;
            return false;
        }

        // Kiểm tra ảnh;
        public static bool KiemTraAnh(int maanh)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            if (db.Anhs.Where(q => q.MaAnh == maanh) != null)
                return true;
            return false;
        }

        // Thêm Ảnh;
        public static void ThemAnh(string tieude, int maalbum, string duongdan)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            Resource.CSDL.Anh a = new Resource.CSDL.Anh();

            a.TieuDeAnh = tieude;
            a.MaAlbum = maalbum;
            a.DuongDan = duongdan;
            db.Anhs.InsertOnSubmit(a);
            db.SubmitChanges();
        }

        // Xóa 1 Ảnh;
        public static void Xoa1Anh(int maanh)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            Resource.CSDL.Anh a = db.Anhs.SingleOrDefault(q => q.MaAnh == maanh);
            db.Anhs.DeleteOnSubmit(a);
            db.SubmitChanges();
        }

        // Xóa tất cả ảnh trong 1 Album;
        public static void XoaHetAnhCuaAlbum(int maalbum)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            var sql = db.Anhs.Where(q => q.MaAlbum == maalbum);
            db.Anhs.DeleteAllOnSubmit(sql);
            db.SubmitChanges();
        }

        // Sửa thông tin ảnh;
        public static void SuaThongTinAnh(int maanh, string tieude, int maalbum, string duongdan)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            Resource.CSDL.Anh a = db.Anhs.SingleOrDefault(q => q.MaAnh == maanh);
            a.TieuDeAnh = tieude;
            a.MaAlbum = maalbum;
            a.DuongDan = duongdan;
            db.SubmitChanges();
        }

        // Chuyển ảnh từ album này sang album khác;
        public static void ChuyenAnh(int maanh, int album)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            Resource.CSDL.Anh oldAnh = db.Anhs.FirstOrDefault(p => p.MaAnh == maanh);
            Resource.CSDL.Anh newAnh = new Resource.CSDL.Anh();
            newAnh.TieuDeAnh = oldAnh.TieuDeAnh;
            newAnh.DuongDan = oldAnh.DuongDan;
            newAnh.MaAlbum = album;
            db.Anhs.DeleteOnSubmit(oldAnh);
            db.Anhs.InsertOnSubmit(newAnh);
            db.SubmitChanges();
        }

        // Lấy thông tin ảnh theo mã ảnh
        public static Resource.CSDL.Anh LayAnh(int maanh)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            Resource.CSDL.Anh a = db.Anhs.SingleOrDefault(q => q.MaAnh == maanh);
            return a;
        }

        // Lấy danh sách album trừ album có mã album;
        public static IQueryable<Resource.CSDL.Album> DanhSachAlbumLoaiTru(int album)
        {
            dbGroupManagerDataContext db = new dbGroupManagerDataContext();
            var ab = db.Albums.Where(q => q.MaAlbum == album);
            var sql = db.Albums.Select(q => q);
            return sql.Except(ab);
        }
    }
}